<?php
	session_start();
	require_once('functions.php');
	
	$name = $_POST['name'];
	$username = $_POST['username'];
	$password = $_POST['password'];
	$password_repeat = $_POST['password_repeat'];
	$age = $_POST['age'];
	
	if(!$name || !$username || !$password || !$password_repeat)
	{	
		$error_msg = "Please fill in all the values marked with an asterisk (*).";
	}
	
	if($password != $password_repeat)
	{	
		$error_msg = "Password and Confirm Password must match.";
	}
	
	if($error_msg)
	{
		header("Location:index.php?response_msg=".$error_msg);
		exit;
	}
	else
	{
		if( add_new_user($username, $password, $name, $age) )
		{
			header("Location:index.php?response_msg=New user added with username: ".htmlspecialchars($username) );
			exit;
		}
		else
		{
			header("Location:index.php?response_msg=An error has occurred. New user could not be added. ".$_SESSION['response_msg']);
			exit;
		}
	}
?>